@frappe.whitelist()
def make_supplier_quotation(source_name, target_doc=None):
    
    mr = frappe.get_doc('Material Request', source_name)

    

    if doc.material_request_type == 'Purchase' and not (doc.purchase_vendor and doc.flags.split_mr):
        item_code_list = [item.item_code for item in doc.items]
        production_plan = doc.items[0].production_plan
        qty_list = frappe.get_all("Item", 
            filters = {'item_code':('in', item_code_list),
                        'min_pack_qty': ('>',0)},
            fields =['item_code','purchase_uom', 'stock_uom', 'min_pack_qty'])
        #转换成字典
        qty_dict = {(item.item_code, item.purchase_uom or item.stock_uom):item.min_pack_qty for item in qty_list}
        item_default_list = frappe.get_all('Item Default', 
                                        filters = {'company': doc.company,
                                                'parent':('in', item_code_list)},
                                        fields=['default_supplier','parent'])
        supplier_item_map = {}
        for item_default in item_default_list:
            item_list = supplier_item_map.setdefault(item_default.default_supplier, [])
            item_list.append(item_default.parent)
        i = False
        for (supplier, item_list) in supplier_item_map.items():
            if not i:
                first_supplier = supplier
                first_mr_item_list = item_list
            else:
                new_mr = frappe.copy_doc(doc,ignore_no_copy=False)
                new_mr.purchase_vendor = supplier
                new_mr.flags.split_mr = True
                items = []
                for row in new_mr.items:
                    if row.item_code in item_list:
                        row.production_plan = production_plan
                        min_pack_qty = qty_dict.get((row.item_code, row.uom), 0)
                        if min_pack_qty:
                            Quotient = row.qty / min_pack_qty
                            rounded_quotient = frappe.utils.floor(Quotient)
                            if Quotient > rounded_quotient:
                                row.qty = (rounded_quotient + 1) * min_pack_qty
                                row.stock_qty = row.qty * row.conversion_factor
                        items.append(row)
                new_mr.items = items
                new_mr.insert()
            i = True
        doc.purchase_vendor = first_supplier
        items = []
        for row in doc.items:
            if row.item_code in first_mr_item_list:
                min_pack_qty = qty_dict.get((row.item_code, row.uom), 0)
                if min_pack_qty:
                    Quotient = row.qty / min_pack_qty
                    rounded_quotient = frappe.utils.floor(Quotient)
                    if Quotient > rounded_quotient:
                        row.qty = (rounded_quotient + 1) * min_pack_qty
                        row.stock_qty = row.qty * row.conversion_factor
                items.append(row)
        doc.items = items
